package com.yootk.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 自定义线程池配置
 */
@Configuration
public class CustomAsyncPoolConfig implements WebMvcConfigurer {

    // 异步配置
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(10000);    // 配置超时时间
        configurer.registerCallableInterceptors(this.getTimeoutInterceptor());  // 设置Callable拦截器
        configurer.setTaskExecutor(this.getAysncThreadPoolTaskExecutor());      // 设置异步线程池
    }

    // 异步线程池配置
    @Bean(name = "asyncPoolTaskExecutor")
    public ThreadPoolTaskExecutor getAysncThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(20);               // 内核线程的个数（物理线程个数 * 2）
        taskExecutor.setMaxPoolSize(200);               // 工作线程池大小
        taskExecutor.setQueueCapacity(25);              // 设置一个延迟队列大小
        taskExecutor.setKeepAliveSeconds(200);          // 存活时间
        taskExecutor.setThreadNamePrefix("yootk - ");   // 配置前缀，自定义的个性化配置
        taskExecutor.setRejectedExecutionHandler(       // 配置拒绝策略
                new ThreadPoolExecutor.CallerRunsPolicy());
        taskExecutor.initialize();  // 线程池初始化配置
        return taskExecutor;
    }

    // 超时配置
    @Bean
    public TimeoutCallableProcessingInterceptor getTimeoutInterceptor() {
        return new TimeoutCallableProcessingInterceptor();
    }
}
